package org.demo.rabbitmq;

import java.util.Arrays;

import lombok.extern.slf4j.Slf4j;
import org.demo.rabbitmq.service.Receiver;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import sun.java2d.pipe.SpanShapeRenderer;

@SpringBootApplication
@Slf4j
public class RabbitmqApplication {
	public static final String queueName = "spring-boot";

	public static void main(String[] args) {
		SpringApplication.run(RabbitmqApplication.class, args);
	}
	
	@Bean
	public Queue queue(){
		return new Queue(queueName, false);
	}

	@Bean
	public TopicExchange exchange() {
	    return new TopicExchange("spring-boot-exchange");
	}

	@Bean
	public Binding binding(Queue queue, TopicExchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with(queueName);
	}

	@Bean
	public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
													MessageListenerAdapter listenerAdapter){
		SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		container.setQueueNames(queueName);
		container.setMessageListener(listenerAdapter);
		return container;
	}

	@Bean
	public MessageListenerAdapter ListenerAdapter(Receiver receiver) {
	    return new MessageListenerAdapter(receiver, "receiveMessage");
	}


}
